package com.sdy.dcsb.web.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
@Slf4j
public class BizInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //登录接口路径
        final String pathLogin = "/SUser/login";
        //用户信息路径
        final String pathUserInfo = "/SUser/userInfo";
        //外对开发接口路径
        final String pathWeb = "DCSBWeb";

        //return true;
        if(pathLogin.equals(request.getRequestURI()) || pathUserInfo.equals(request.getRequestURI())) {
            return true;
        }else if(request.getRequestURI().contains(pathWeb)) {
            return true;
        }else {
            request.setCharacterEncoding("UTF-8");
            HttpSession session = request.getSession(false);
            if(session == null){
                response.setHeader("dcsbstatus","false");
                return false;
            }
            session.setMaxInactiveInterval(20*60);

            Object user = session.getAttribute("user");
            if(user == null){
                response.setHeader("dcsbstatus","false");
                return false;
            }
            return true;
        }

    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        super.postHandle(request,response,handler,modelAndView);
    }

}
